#include "tcpsocket.h"
Tcpserver* Tcpsocket::server = nullptr;

Tcpsocket::Tcpsocket(bufferevent *ev, Tcpserver *server, char *ip, uint16_t prot)
{
    this->ev=ev;
    this->ip=ip;
    this->prot=prot;
    this->server=server;
    this->userName="";
}

void Tcpsocket::Readeventcb(bufferevent *bev, void *ctx)
{
    Tcpsocket*s=(Tcpsocket*)ctx;
    server->readEvent(s);

}

void Tcpsocket::Writeevnetcb(bufferevent *bev, void *ctx)
{
    Tcpsocket*s=(Tcpsocket*)ctx;
    server->writeEvent(s);
}

void Tcpsocket::Closeeventcb(bufferevent *bev, short what, void *ctx)
{
    Tcpsocket*s=(Tcpsocket*)ctx;
    server->closeEvent(s,what);
    delete s;
}

char *Tcpsocket::getip()
{
    return ip;
}

u_int16_t Tcpsocket::getPort()
{
    return prot;
}


int Tcpsocket::read(void *data, int size)
{
    return bufferevent_read(ev,data,size);
}
int Tcpsocket::write(void *data, int size)
{
    return bufferevent_write(ev,data,size);
}

void Tcpsocket::setuserName(std::string name)
{
    this->userName=name;
}

std::string Tcpsocket::getuserName()
{
    return userName;
}
